<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\model\RlDistributionOfficer;
use plugin\admin\app\model\RlMember;
use plugin\admin\app\model\RlMemberWallet;
use support\Db;
use support\Request;
use support\Response;
use plugin\admin\app\model\RlDistributionOfficersLog;
use support\exception\BusinessException;
use const support\CORRECT;
use const support\DISTRIBUTION_RATE_DEFAULT;
use const support\wallet_types_1;

/**
 * 申请审核列表 
 */
class RlDistributionOfficersLogController extends Crud
{
    
    /**
     * @var RlDistributionOfficersLog
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new RlDistributionOfficersLog;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('rl-distribution-officers-log/index');
    }

    /**
     * 获取
     * @return Response
     */
    public function select(Request $request): Response
    {
        $page = (int)$request->get('page');
        $limit = (int)$request->get('limit', 10);
        $where['status']=1;
        if ($id=$request->get('id','')){
            $where['_id']=$id;
        }
        $count=RlDistributionOfficersLog::whereIn('status',[0,1])->count();
        $offset=($page-1)*$limit;
        $lists= RlDistributionOfficersLog::whereIn('status',[0,1])->orderBy('status')
            ->offset($offset)->limit($limit)->get()->toArray();
        //获取用户ID
        $memberIds=array_column($lists,'member_id');
        $members=RlMember::whereIn('id',$memberIds)->get()->toArray();
        $members=array_column($members,null,'id');
        //进行昵称头像追加
        foreach ($lists as &$list){
            $list['nickname']=$members[$list['member_id']]['nickname']??'';
            $list['avatar']=$members[$list['member_id']]['avatar']??'';
        }
        return json(['code' => 0, 'data' => $lists, 'count'=>$count,'msg' => '成功']);
    }


    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function audit(Request $request): Response
    {
        if ($request->method() === 'POST') {
            //接收前端传值
            $id=$request->post('id','');
            $status=$request->post('status',2);
            $distributionRate=$request->post('distribution_rate')?:DISTRIBUTION_RATE_DEFAULT;
            $info=RlDistributionOfficersLog::where([
                'id'=>$id
            ])->first()->toArray();
            DB::beginTransaction();
            try {
                $ret=RlDistributionOfficersLog::where([
                    'id'=>$id
                ])->update([
                    'status'=>$status,
                    'update_time'=>time()
                ]);
                if (!$ret){
                    DB::rollBack();
                    return $this->json(-1,'操作失败1');
                }
                if ($status){
                    //创建分销人员
                    $ret=RlDistributionOfficer::insertGetId([
                        'member_id'=>$info['member_id'],
                        'real_name'=>$info['real_name'],
                        'distribution_rate'=>$distributionRate,
                        'mobile'   =>$info['mobile'],
                        'status'   =>1,
                        'create_time'=>time()
                    ]);
                    if (!$ret){
                        DB::rollBack();
                        return $this->json(-1,'操作失败2');
                    }
                    //为分销人员开账户
                    $ret=RlMemberWallet::updateOrInsert([
                        'wallet_type'=>wallet_types_1,
                        'member_id'  =>$info['member_id'],
                        'status'     =>CORRECT
                    ],[
                        'update_time'=>time(),
                        'create_time'=>time()
                    ]);
                    if (!$ret){
                        DB::rollBack();
                        return $this->json(-1,'操作失败3');
                    }
                }
                DB::commit();
            }catch (\Throwable $e){
                DB::rollBack();
                return $this->json(-1,'操作失败0');
            }
            return $this->json(0);
        }
        return view('rl-distribution-officers-log/audit');
    }

}
